﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http.Dependencies;
using Autofac;
using Autofac.Integration.WebApi;

namespace System.Net.Http
{
    /// <summary>
    /// HttpRequestMessageExtension
    /// </summary>
    public static partial class HttpRequestMessageExtension
    {
        /// <summary>
        /// 获取或设置 HTTP 响应上的 Content-Length 内容标头值
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public static long GetContentLength(this HttpRequestMessage request)
        {
            var length= request.Content.Headers.ContentLength;
            return length.HasValue ? length.Value : 0L;
        }
        /// <summary>
        /// 获取实例
        /// </summary>
        /// <typeparam name="TService"></typeparam>
        /// <param name="request"></param>
        /// <returns></returns>
        public static TService GetService<TService>(this HttpRequestMessage request)
        {
            return (TService)request.GetDependencyScope().GetService(typeof(TService));
            //IDependencyScope dependencyScope = request.GetDependencyScope();
            //ILifetimeScope requestLifetimeScope = dependencyScope.GetRequestLifetimeScope();
            //return requestLifetimeScope.Resolve<TService>();
        }
       
        /// <summary>
        /// 获取当前请求关联的属性集
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="request"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static T GetProperty<T>(this HttpRequestMessage request, string key)
        {
            object value;
            if (request.Properties.TryGetValue(key, out value))
                return (T)value;
            return default(T);
        }

        /// <summary>
        /// Retrieves an individual cookie from the cookies collection
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cookieName"></param>
        /// <returns></returns>
        public static string GetCookie(this HttpRequestMessage request, string cookieName)
        {
            CookieHeaderValue cookie = request.Headers.GetCookies(cookieName).FirstOrDefault();
            if (cookie != null)
                return cookie[cookieName].Value;

            return null;
        }



        private const string HttpContext = "MS_HttpContext";
        private const string RemoteEndpointMessage = "System.ServiceModel.Channels.RemoteEndpointMessageProperty";
        private const string OwinContext = "MS_OwinContext";

        /// <summary>
        /// 获取客户端IP
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public static string GetClientIP(this HttpRequestMessage request)
        {
            // Web-hosting. Needs reference to System.Web.dll
            if (request.Properties.ContainsKey(HttpContext))
            {
                dynamic ctx = request.Properties[HttpContext];
                if (ctx != null)
                {
                    return ctx.Request.UserHostAddress;
                }
            }

            // Self-hosting. Needs reference to System.ServiceModel.dll. 
            if (request.Properties.ContainsKey(RemoteEndpointMessage))
            {
                dynamic remoteEndpoint = request.Properties[RemoteEndpointMessage];
                if (remoteEndpoint != null)
                {
                    return remoteEndpoint.Address;
                }
            }

            // Self-hosting using Owin. Needs reference to Microsoft.Owin.dll. 
            if (request.Properties.ContainsKey(OwinContext))
            {
                dynamic owinContext = request.Properties[OwinContext];
                if (owinContext != null)
                {
                    return owinContext.Request.RemoteIpAddress;
                }
            }

            return string.Empty;
        }


    }
}
